//
//  AITopicTopView.m
//  Mia
//
//  Created by Vill on 2024/2/21.
//

#import "AITopicTopView.h"
#import "XDServiceTagFlowLayout.h"
#import "AITopicTopCollectionCell.h"

@interface AITopicTopView ()<UICollectionViewDelegate,UICollectionViewDataSource,XDServiceTagFlowLayoutDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation AITopicTopView

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        XDServiceTagFlowLayout * layout = [[XDServiceTagFlowLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsMake(0, PTW(20), 0, PTW(20));
        layout.minimumLineSpacing = PTW(10);
        layout.minimumInteritemSpacing = PTW(10);
        layout.numberOfColumns = 1;
        layout.delegate = self;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.userInteractionEnabled = YES;
        [_collectionView registerClass:[AITopicTopCollectionCell class] forCellWithReuseIdentifier:@"AITopicTopCollectionCell"];
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _collectionView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if(self == [super initWithFrame:frame]){
        self.backgroundColor = UIColor.clearColor;
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.mas_equalTo(0);
            make.top.mas_equalTo(0).mas_offset(PTW(10));
            make.bottom.mas_equalTo(0).mas_offset(-PTW(10));
        }];
    }
    return self;
}

- (void)setTopicArr:(NSMutableArray *)topicArr{
    _topicArr = topicArr;
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource, UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.topicArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    AITopicTopCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AITopicTopCollectionCell" forIndexPath:indexPath];
    AITopicChatTopModel *model = self.topicArr[indexPath.item];
    cell.btnStr = model.name;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    AITopicChatTopModel *model = self.topicArr[indexPath.item];
    [self.topicArr removeObjectAtIndex:indexPath.row];
    [self.collectionView reloadData];
    if (self.certainBlock) {
        self.certainBlock(model);
    }
}

- (CGFloat)waterFlowLayout:(XDServiceTagFlowLayout *)layout widthForItem:(NSIndexPath *)indexPath{
    AITopicChatTopModel *model = self.topicArr[indexPath.item];
    return [AIUIHelperManager getLabelWidth:model.name defaultHeight:PTW(30) font:[UIFont fontWithWeight:Medium size:PTW(12)]] + PTW(20);
}


@end
